<template>
  <div class="Search">
    <div class="SearchBox">
      <van-search
        v-model="value"
        show-action
        placeholder="点击搜索商品"
        @search="onSearch"
      >
        <template #action>
          <div @click="onSearch">搜索</div>
        </template>
      </van-search>
    </div>
    <div class="hotSearch" v-if="hotSearch">
      <p class="title">热门搜索</p>
      <div>
        <span
          v-for="(item, index) in hotSearch"
          :key="index"
          @click="Searching(item)"
          >{{ item }}</span
        >
      </div>
    </div>
    <template v-if="Searchnow">
      <div class="SearchResult">
        <div class="Searchresultlist">

        <HotGoodsCard v-for="item in SearchResult" :item="item" :key="item.id"></HotGoodsCard>
        </div>
        <template v-if="SearchResult == ''">
        <div class="empty" >
          <van-empty
            class="custom-image"
            image="http://47.115.51.185/h5/img/noGood.7aec7edd.png"
          />
        </div>
      <RecommendForYou v-show="recommend"></RecommendForYou>
      </template>
      </div>
    </template>
    
  </div>
</template>

<script>
import RecommendForYou from "@/components/RecommendForYou";
import HotGoodsCard from "@/components/HotGoodsCard";
export default {
  components: {
    RecommendForYou,
    HotGoodsCard,
  },
  data() {
    return {
      hotSearch: null,
      value: "",
      SearchResult: null,
      recommend: false,
      Searchnow: false,
    };
  },
  created() {
    this.axios.get("/api/search/keyword").then((response) => {
      this.hotSearch = response.data.data;
    });
  },
  methods: {
    onSearch() {
      this.Searching(this.value);
    },
    Searching(keyword) {
        if(keyword == "") return;
      this.Searchnow = false;
      this.recommend = true;
      this.axios
        .get("/api/products?page=1&limit=20&keyword=" + keyword)
        .then((response) => {
          this.SearchResult = response.data.data;
            this.Searchnow = true;
        });
    },
  },
};
</script>

<style lang="less">
.Search {
  width: 100%;
  background-color: #f6f6f6;
  .SearchBox {
    width: 100%;
    .van-search__content {
      border-radius: 20px;
      .van-icon-search {
        font-size: 0.4rem;
      }
    }
  }
  .hotSearch {
    width: 100%;
    background-color: #fff;
    padding-top: 10px;
    padding-left: 10px;
    padding-right: 10px;
    padding-bottom: 20px;
    border-bottom: 1px solid #f4f4f4;
    .title {
      font-size: 14px;
      color: #999999;
    }
    > div {
      width: 100%;
      margin-top: 15px;
      display: flex;
      flex-wrap: wrap;
      span {
        border: 1px solid #d4d4d4;
        padding: 6px 10px;
        color: #454545;
        margin-right: 10px;
        font-size: 14px;
      }
    }
  }
  .SearchResult {
    width: 100%;
    background-color: #fff;
    .Searchresultlist{
      width: 100%;
      padding: 0 10px;
    }
    .empty {
      width: 100%;
      .van-empty {
        width: 100%;
        background-color: #fff;
        .van-empty__image {
          width: 207px;
          height: auto;
        }
      }
    }
  }
  
}
</style>